<?php
namespace qick;
use qick\qick_error;
require('qick/config/env.php');
header("Content-type: text/html; charset=utf-8");


class webcore
{
    public static $routeMessage = array(
        'cl' => 'index',
        'mth' => 'index',
        'param' => array()
    );

    public static function loader()
    {
        spl_autoload_register(function($class) {
            $class = strtr($class, '\\', '/');
            if (file_exists("{$class}.php")) {
                echo $class;
                require("{$class}.php");
            }
            else {
                qick_error::put(500, function($prt) use ($class) {
                    $prt("web 核心异常: 无法加载文件 {$class}.php");
                });
            }
        });
    }

/**
 * 路由策略
 */
    public static function router($model='GET')
    {
        // GET 路由
        if ($model === 'GET')
        {
            $_cl = empty($_GET['c']) ? 'index' : $_GET['c'];
            $aResult['cl'] = preg_replace('/^\/|\/$/', '', $_cl);
            $aResult['mth'] = empty($_GET['m']) ? 'index' : $_GET['m'];
            $aResult['param'] = array();
            self::$routeMessage = $aResult;
        }
        // URL 路由
        elseif ($model === 'URL') {
            $_cl = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
            $splitUrl = preg_replace('/^\/|\/?$/', '', $_cl);

            require('qick/config/route.php');
            if (! empty($splitUrl)) {
                foreach ($router as $url_match => $package) {
                    $url_match = str_replace(array(':any', ':num'), array('[^\/]+', '[0-9]+'), $url_match);
                    $url_match = str_replace('/', '\\/', $url_match);
                    // 拼接正则首尾
                    $url_match = sprintf('/^%s\/?$/', $url_match);
                    if (preg_match($url_match, $splitUrl, $aResult['param'])) {
                        array_shift($aResult['param']);
                        $_cl = str_replace(array(':any', ':num'), array('[^\/]+', '[0-9]+'), $package['class']);
                        $aResult['cl'] = $_cl;
                        $aResult['mth'] = $package['method'];
                        self::$routeMessage = $aResult;
                        return true;
                    }
                }
                qick_error::put(404, function($prt) {
                    $prt('你可能访问了假页面');
                });
            }
            else {
                if (isset($router['default'])) {
                    $aResult['cl'] = $router['default']['class'];
                    $aResult['mth'] = $router['default']['method'];
                    $aResult['param'] = array();
                }
                else {
                    $aResult['cl'] = 'index';
                    $aResult['mth'] = 'index';
                    $aResult['param'] = array();
                }
            }
            self::$routeMessage = $aResult;
        }
        else {
            qick_error::put(500, function($prt) {
                $prt('web 核心异常: 路由策略错误, 请选择正确的路由策略');
            });
        }
    }

/**
 * web 页面缓存
 */
    public static function web_catch($model)
    {
        if ($model === 'memcatch') {
            webkit::mem_catch(self::$routeMessage['cl'], self::$routeMessage['mth']);
        }
        elseif ($model === 'redis') {
            webkit::redis_catch(self::$routeMessage['cl'], self::$routeMessage['mth']);
        }
        else {
            qick_error::put(500, function($prt) {
                $prt('web 核心异常: 页面缓存策略错误, 请选择正确的缓存策略');
            });
        }
    }

/**
 * 调用 webcore::$routeMessage 中的模块并向模块传递参数
 * webcore::$routeMessage['cl'] 包名称(包含地址)
 * webcore::$routeMessage['mth'] 预执行的方法
 * webcore::$routeMessage['param'] 一维数组，向方法内传递的参数
 */
    public static function action()
    {
        $cl = preg_replace('/^\/|\/$/', '', self::$routeMessage['cl']);
        $app = str_replace('/', '\\', 'run/'. $cl);  // 包名称

        // 调用传参
        call_user_func_array(
            array(new $app, self::$routeMessage['mth']),
            self::$routeMessage['param']
        );
    }

}
